<?php
class Mfrm extends CI_Model {

    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    
    function create_journal_log_account($id_frm_journal, $id_frm_journal_log, $debit, $debit_account, $kredit, $kredit_account, $penyesuaian = ""){
      if($penyesuaian){
        $last = $penyesuaian;
      }
      else{
        $last = "_percobaan";
      }
      foreach($debit AS $key => $dp){
        $kirim_log_account[] = array(
            "id_frm_account"    => $debit_account[$key],
            "id_frm_journal"    => $id_frm_journal,
            "id_frm_journal_log{$penyesuaian}" => $id_frm_journal_log,
            "saldo"             => $dp,
            "pos"               => 1,
            "create_by_users"   => $this->session->userdata("id"),
            "create_date"       => date("Y-m-d H:i:s")
        );
    
        $pos_asli = $this->get_field("frm_account", "pos", array("id_frm_account" => $debit_account[$key]));
        $cek_neraca = $this->get_field("frm_neraca{$last}", "id_frm_neraca{$last}", 
                array("id_frm_account" => $debit_account[$key], "id_frm_journal" => $id_frm_journal));
        
        if($pos_asli == 1){
          $price_debit = $dp;
        }
        else{
          $price_debit = $dp * -1;
        }
        
        if($cek_neraca){
          $price_neraca = $this->get_field("frm_neraca{$last}", "saldo", 
                array("id_frm_neraca{$last}" => $cek_neraca));
          $kirim_neraca_percobaan = array(
              "id_frm_account"        => $debit_account[$key],
              "id_frm_journal"        => $id_frm_journal,
              "saldo"                 => ($price_neraca + $price_debit),
              "pos"                   => $pos_asli,
              "update_by_users"       => $this->session->userdata("id"),
          );
          $id_frm_neraca_percobaan = $this->update("frm_neraca{$last}", array("id_frm_neraca{$last}" => $cek_neraca), $kirim_neraca_percobaan);
        }
        else{
          $kirim_neraca_percobaan = array(
              "id_frm_account"        => $debit_account[$key],
              "id_frm_journal"        => $id_frm_journal,
              "saldo"                 => $price_debit,
              "pos"                   => $pos_asli,
              "create_by_users"       => $this->session->userdata("id"),
              "create_date"           => date("Y-m-d H:i:s")
          );
          $id_frm_neraca_percobaan = $this->db->insert("frm_neraca{$last}", $kirim_neraca_percobaan);
        }
      }
      foreach($kredit AS $key => $kp){
        $kirim_log_account[] = array(
            "id_frm_account"    => $kredit_account[$key],
            "id_frm_journal"    => $id_frm_journal,
            "id_frm_journal_log{$penyesuaian}" => $id_frm_journal_log,
            "saldo"             => $kp,
            "pos"               => 2,
            "create_by_users"   => $this->session->userdata("id"),
            "create_date"       => date("Y-m-d H:i:s")
        );
            
        $pos_asli = $this->get_field("frm_account", "pos", array("id_frm_account" => $kredit_account[$key]));
        $cek_neraca = $this->get_field("frm_neraca{$last}", "id_frm_neraca{$last}", 
                array("id_frm_account" => $kredit_account[$key], "id_frm_journal" => $id_frm_journal));
        
        if($pos_asli == 2){
          $price_debit = $kp;
        }
        else{
          $price_debit = $kp * -1;
        }
        
        if($cek_neraca){
          $price_neraca = $this->get_field("frm_neraca{$last}", "saldo", 
                array("id_frm_neraca{$last}" => $cek_neraca));
          $kirim_neraca_percobaan = array(
              "id_frm_account"        => $kredit_account[$key],
              "id_frm_journal"        => $id_frm_journal,
              "saldo"                 => ($price_neraca + $price_debit),
              "pos"                   => $pos_asli,
              "update_by_users"       => $this->session->userdata("id"),
          );
          $id_frm_neraca_percobaan = $this->update("frm_neraca{$last}", array("id_frm_neraca{$last}" => $cek_neraca), $kirim_neraca_percobaan);
        }
        else{
          $kirim_neraca_percobaan = array(
              "id_frm_account"        => $kredit_account[$key],
              "id_frm_journal"        => $id_frm_journal,
              "saldo"                 => $price_debit,
              "pos"                   => $pos_asli,
              "create_by_users"       => $this->session->userdata("id"),
              "create_date"           => date("Y-m-d H:i:s")
          );
          $id_frm_neraca_percobaan = $this->db->insert("frm_neraca{$last}", $kirim_neraca_percobaan);
        }
            
      }
      $this->db->insert_batch("frm_journal_log_account{$penyesuaian}", $kirim_log_account);
      return true;
    }
    
    function create_journal_log_account_yayasan($id_keuangan_yayasan_journal, $id_keuangan_yayasan_journal_log, $debit, $debit_account, $kredit, $kredit_account, $penyesuaian = ""){
      
      if($penyesuaian){
        $last = $penyesuaian;
      }
      else{
        $last = "_percobaan";
      }
      foreach($debit AS $key => $dp){
        $kirim_log_account[] = array(
            "id_frm_account"    => $debit_account[$key],
            "id_keuangan_yayasan_journal"    => $id_keuangan_yayasan_journal,
            "id_keuangan_yayasan_journal_log{$penyesuaian}" => $id_keuangan_yayasan_journal_log,
            "saldo"             => $dp,
            "pos"               => 1,
            "create_by_users"   => $this->session->userdata("id"),
            "create_date"       => date("Y-m-d H:i:s")
        );
    
        $pos_asli = $this->get_field("frm_account", "pos", array("id_frm_account" => $debit_account[$key]));
        $cek_neraca = $this->get_field("keuangan_yayasan_neraca{$last}", "id_keuangan_yayasan_neraca{$last}", 
                array("id_frm_account" => $debit_account[$key], "id_keuangan_yayasan_journal" => $id_keuangan_yayasan_journal));
        
        if($pos_asli == 1){
          $price_debit = $dp;
        }
        else{
          $price_debit = $dp * -1;
        }
        
        if($cek_neraca){
          $price_neraca = $this->get_field("keuangan_yayasan_neraca{$last}", "saldo", 
                array("id_keuangan_yayasan_neraca{$last}" => $cek_neraca));
          $kirim_neraca_percobaan = array(
              "id_frm_account"        => $debit_account[$key],
              "id_keuangan_yayasan_journal"        => $id_keuangan_yayasan_journal,
              "saldo"                 => ($price_neraca + $price_debit),
              "pos"                   => $pos_asli,
              "update_by_users"       => $this->session->userdata("id"),
          );
          $id_keuangan_yayasan_neraca_percobaan = $this->update("keuangan_yayasan_neraca{$last}", array("id_keuangan_yayasan_neraca{$last}" => $cek_neraca), $kirim_neraca_percobaan);
        }
        else{
          $kirim_neraca_percobaan = array(
              "id_frm_account"        => $debit_account[$key],
              "id_keuangan_yayasan_journal"        => $id_keuangan_yayasan_journal,
              "saldo"                 => $price_debit,
              "pos"                   => $pos_asli,
              "create_by_users"       => $this->session->userdata("id"),
              "create_date"           => date("Y-m-d H:i:s")
          );
          $id_keuangan_yayasan_neraca_percobaan = $this->db->insert("keuangan_yayasan_neraca{$last}", $kirim_neraca_percobaan);
        }
      }
      foreach($kredit AS $key => $kp){
        $kirim_log_account[] = array(
            "id_frm_account"    => $kredit_account[$key],
            "id_keuangan_yayasan_journal"    => $id_keuangan_yayasan_journal,
            "id_keuangan_yayasan_journal_log{$penyesuaian}" => $id_keuangan_yayasan_journal_log,
            "saldo"             => $kp,
            "pos"               => 2,
            "create_by_users"   => $this->session->userdata("id"),
            "create_date"       => date("Y-m-d H:i:s")
        );
            
        $pos_asli = $this->get_field("frm_account", "pos", array("id_frm_account" => $kredit_account[$key]));
        $cek_neraca = $this->get_field("keuangan_yayasan_neraca{$last}", "id_keuangan_yayasan_neraca{$last}", 
                array("id_frm_account" => $kredit_account[$key], "id_keuangan_yayasan_journal" => $id_keuangan_yayasan_journal));
        
        if($pos_asli == 2){
          $price_debit = $kp;
        }
        else{
          $price_debit = $kp * -1;
        }
        
        if($cek_neraca){
          $price_neraca = $this->get_field("keuangan_yayasan_neraca{$last}", "saldo", 
                array("id_keuangan_yayasan_neraca{$last}" => $cek_neraca));
          $kirim_neraca_percobaan = array(
              "id_frm_account"        => $kredit_account[$key],
              "id_keuangan_yayasan_journal"        => $id_keuangan_yayasan_journal,
              "saldo"                 => ($price_neraca + $price_debit),
              "pos"                   => $pos_asli,
              "update_by_users"       => $this->session->userdata("id"),
          );
          $id_keuangan_yayasan_neraca_percobaan = $this->update("keuangan_yayasan_neraca{$last}", array("id_keuangan_yayasan_neraca{$last}" => $cek_neraca), $kirim_neraca_percobaan);
        }
        else{
          $kirim_neraca_percobaan = array(
              "id_frm_account"        => $kredit_account[$key],
              "id_keuangan_yayasan_journal"        => $id_keuangan_yayasan_journal,
              "saldo"                 => $price_debit,
              "pos"                   => $pos_asli,
              "create_by_users"       => $this->session->userdata("id"),
              "create_date"           => date("Y-m-d H:i:s")
          );
          $id_keuangan_yayasan_neraca_percobaan = $this->db->insert("keuangan_yayasan_neraca{$last}", $kirim_neraca_percobaan);
        }
            
      }
      $this->db->insert_batch("keuangan_yayasan_journal_log_account{$penyesuaian}", $kirim_log_account);
      return true;
    }
    
    function update($table, $where, $kirim){
      $this->db->where($where);
      return $this->db->update($table, $kirim);
    }
    
    function get_field($table, $field, $where = array()){
      $this->db->select($field." as a");
      if($where)
        $this->db->where($where);
      $w = $this->db->get($table)->row();
      if($w)
        return $w->a;
      else
        return false;
    }
    
    function create_journal_log($pst, $table = "frm_journal_log"){
      $kirim = array(
          "id_frm_journal"      => $pst['id_frm_journal'],
          "notransaksi"         => $pst['notransaksi'],
          "saldo"               => $pst['saldo'],
          "status"              => 1,
          "tanggal"             => $pst['tanggal'],
          "note"                => $pst['note'],
          "create_by_users"     => $this->session->userdata("id"),
          "create_date"         => date("Y-m-d H:i:s")
      );
      $this->db->insert($table, $kirim);
      return $this->db->insert_id();
    }
    
    function create_journal_log_yayasan($pst, $table = "keuangan_yayasan_journal_log"){
      $kirim = array(
          "id_keuangan_yayasan_journal"      => $pst['id_keuangan_yayasan_journal'],
          "notransaksi"         => $pst['notransaksi'],
          "saldo"               => $pst['saldo'],
          "status"              => 1,
          "tanggal"             => $pst['tanggal'],
          "note"                => $pst['note'],
          "create_by_users"     => $this->session->userdata("id"),
          "create_date"         => date("Y-m-d H:i:s")
      );
      $this->db->insert($table, $kirim);
      return $this->db->insert_id();
    }
    
    function get_journal_current(){
      $this->db->select("id_frm_journal, id_next");
      $this->db->where(array(
          "status"             => 1
      ));
//      $this->db->where(array(
//          "month"             => date("m"),
//          "year"              => date("Y")
//      ));
      $hasil = $this->db->get("frm_journal")->row();
      if($hasil->id_next){
        $hasil = $this->cek_next($hasil->id_next);
      }
      return $hasil->id_frm_journal;
    }
    
    function cek_next($id_frm_journal){
      $this->db->select("id_frm_journal, id_next");
      $this->db->where(array(
          "id_frm_journal"  => $id_frm_journal,
      ));
      $hasil = $this->db->get("frm_journal")->row();
      if($hasil->id_next)
        $hasil = $this->cek_next($hasil->id_next);
      
      return $hasil;
    }
    
    function get_tunggakan($nim){
      /**
       * plus artinya ada tunggakan
       * minus artinya ada kelebihan
       */
      $total_debit = $this->db->query("
        SELECT SUM(A.saldo) AS saldo
        FROM keuangan_mahasiswa_tanggungan AS A
        WHERE A.nim = '{$nim}' AND A.pos = 1
        ")->result();

      $total_kredit = $this->db->query("
        SELECT SUM(A.saldo) AS saldo
        FROM keuangan_mahasiswa_tanggungan AS A
        WHERE A.nim = '{$nim}' AND A.pos = 2
        ")->result();
        
      $hasil = $total_debit[0]->saldo - $total_kredit[0]->saldo;
        
      return $hasil;
    }
    
    function get_tunggakan_pos($nim, $pos){
      /**
       * plus artinya ada tunggakan
       * minus artinya ada kelebihan
       */
      $total = $this->db->query("
        SELECT SUM(A.saldo) AS saldo
        FROM keuangan_mahasiswa_tanggungan AS A
        WHERE A.nim = '{$nim}' AND A.pos = $pos
        ")->result();

      $hasil = $total[0]->saldo;
        
      return $hasil;
    }
    
    function get_tunggakan_komponen($nim, $id_keuangan_mahasiswa_jenis_virtual_account){
      /**
       * plus artinya ada tunggakan
       * minus artinya ada kelebihan
       */
      $total_debit = $this->db->query("
        SELECT SUM(A.saldo) AS saldo
        FROM keuangan_mahasiswa_tanggungan AS A
        WHERE A.nim = '{$nim}' AND A.pos = 1 AND id_keuangan_mahasiswa_jenis_virtual_account = $id_keuangan_mahasiswa_jenis_virtual_account
        ")->result();

      $total_kredit = $this->db->query("
        SELECT SUM(A.saldo) AS saldo
        FROM keuangan_mahasiswa_tanggungan AS A
        WHERE A.nim = '{$nim}' AND A.pos = 2 AND id_keuangan_mahasiswa_jenis_virtual_account = $id_keuangan_mahasiswa_jenis_virtual_account
        ")->result();
        
      $total_tarikan = $this->db->query("
        SELECT SUM(A.saldo) AS saldo
        FROM keuangan_mahasiswa_tanggungan AS A
        WHERE A.nim = '{$nim}' AND A.pos = 3 AND id_keuangan_mahasiswa_jenis_virtual_account = $id_keuangan_mahasiswa_jenis_virtual_account
        ")->result();
        
      $hasil = $total_debit[0]->saldo + $total_tarikan[0]->saldo - $total_kredit[0]->saldo;
        
      return $hasil;
    }
    
    function get_tunggakan_mahasiswa_baru($id_ao_applicant){
      $total_debit = $this->db->query("
        SELECT SUM(A.saldo) AS saldo
        FROM keuangan_mahasiswa_baru_tanggungan AS A
        WHERE A.id_ao_applicant = '{$id_ao_applicant}' AND A.pos = 1
        ")->result();

      $total_kredit = $this->db->query("
        SELECT SUM(A.saldo) AS saldo
        FROM keuangan_mahasiswa_baru_tanggungan AS A
        WHERE A.id_ao_applicant = '{$id_ao_applicant}' AND A.pos = 2
        ")->result();
        
      $hasil = $total_debit[0]->saldo - $total_kredit[0]->saldo;
        
      return $hasil;
    }
    
    function get_saldo_buku_besar($id_frm_journal, $id_frm_account){
      $pos_asli = $this->get_field("frm_account", "pos", array("id_frm_account" => $id_frm_account));
      $debit = $this->get_field("frm_journal_log_account", "sum(saldo)", 
              array("id_frm_account" => $id_frm_account, "id_frm_journal" => $id_frm_journal, "pos" => 1));
      $debit += $this->get_field("frm_journal_log_account_penyesuaian", "sum(saldo)", 
              array("id_frm_account" => $id_frm_account, "id_frm_journal" => $id_frm_journal, "pos" => 1));
      $kredit = $this->get_field("frm_journal_log_account", "sum(saldo)", 
              array("id_frm_account" => $id_frm_account, "id_frm_journal" => $id_frm_journal, "pos" => 2));
      $kredit += $this->get_field("frm_journal_log_account_penyesuaian", "sum(saldo)", 
              array("id_frm_account" => $id_frm_account, "id_frm_journal" => $id_frm_journal, "pos" => 2));
//      print $id_frm_account;
//      print $kredit;die;
      if($pos_asli == 1){
        return ($debit - $kredit);
      }
      else{
        return ($kredit - $debit);
      }
    }
    
    function get_saldo_buku_besar_yayasan($id_keuangan_yayasan_journal, $id_frm_account){
      $pos_asli = $this->get_field("frm_account", "pos", array("id_frm_account" => $id_frm_account));
      $debit = $this->get_field("keuangan_yayasan_journal_log_account", "sum(saldo)", 
              array("id_frm_account" => $id_frm_account, "id_keuangan_yayasan_journal" => $id_keuangan_yayasan_journal, "pos" => 1));
      $debit += $this->get_field("keuangan_yayasan_journal_log_account_penyesuaian", "sum(saldo)", 
              array("id_frm_account" => $id_frm_account, "id_keuangan_yayasan_journal" => $id_keuangan_yayasan_journal, "pos" => 1));
      $kredit = $this->get_field("keuangan_yayasan_journal_log_account", "sum(saldo)", 
              array("id_frm_account" => $id_frm_account, "id_keuangan_yayasan_journal" => $id_keuangan_yayasan_journal, "pos" => 2));
      $kredit += $this->get_field("keuangan_yayasan_journal_log_account_penyesuaian", "sum(saldo)", 
              array("id_frm_account" => $id_frm_account, "id_keuangan_yayasan_journal" => $id_keuangan_yayasan_journal, "pos" => 2));
//      print $id_frm_account;
//      print $kredit;die;
      if($pos_asli == 1){
        return ($debit - $kredit);
      }
      else{
        return ($kredit - $debit);
      }
    }
    
    function get_total_pendapatan($id_frm_journal){
      $jumlah_debit = $this->db->query("
        SELECT SUM(A.saldo) AS price
        FROM frm_neraca_percobaan AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_frm_journal = {$id_frm_journal}
          AND B.labarugi = 2 AND B.profit = 1 AND B.pos = 2
        ")->row();
      $jumlah_debit_penyesuaian = $this->db->query("
        SELECT SUM(A.saldo) AS price
        FROM frm_neraca_penyesuaian AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_frm_journal = {$id_frm_journal}
          AND B.labarugi = 2 AND B.profit = 1 AND B.pos = 2
        ")->row();
      $debit = $jumlah_debit->price + $jumlah_debit_penyesuaian->price;
      return $debit;
    }
    
    function get_total_pendapatan_yayasan($id_keuangan_yayasan_journal){
      $jumlah_debit = $this->db->query("
        SELECT SUM(A.saldo) AS price
        FROM keuangan_yayasan_neraca_percobaan AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
          AND B.labarugi = 2 AND B.profit = 1 AND B.pos = 2
        ")->row();
      $jumlah_debit_penyesuaian = $this->db->query("
        SELECT SUM(A.saldo) AS price
        FROM keuangan_yayasan_neraca_penyesuaian AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
          AND B.labarugi = 2 AND B.profit = 1 AND B.pos = 2
        ")->row();
      $debit = $jumlah_debit->price + $jumlah_debit_penyesuaian->price;
      return $debit;
    }
    
    function get_total_pendapatan_lain($id_frm_journal){
      $jumlah_debit = $this->db->query("
        SELECT SUM(A.saldo) AS price
        FROM frm_neraca_percobaan AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_frm_journal = {$id_frm_journal}
          AND B.labarugi = 2 AND B.profit = 2 AND B.pos = 2
        ")->row();
      $jumlah_debit_penyesuaian = $this->db->query("
        SELECT SUM(A.saldo) AS price
        FROM frm_neraca_penyesuaian AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_frm_journal = {$id_frm_journal}
          AND B.labarugi = 2 AND B.profit = 2 AND B.pos = 2
        ")->row();
      $debit = $jumlah_debit->price + $jumlah_debit_penyesuaian->price;
      return $debit;
    }
    
    function get_total_pendapatan_lain_yayasan($id_keuangan_yayasan_journal){
      $jumlah_debit = $this->db->query("
        SELECT SUM(A.saldo) AS price
        FROM keuangan_yayasan_neraca_percobaan AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
          AND B.labarugi = 2 AND B.profit = 2 AND B.pos = 2
        ")->row();
      $jumlah_debit_penyesuaian = $this->db->query("
        SELECT SUM(A.saldo) AS price
        FROM keuangan_yayasan_neraca_penyesuaian AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
          AND B.labarugi = 2 AND B.profit = 2 AND B.pos = 2
        ")->row();
      $debit = $jumlah_debit->price + $jumlah_debit_penyesuaian->price;
      return $debit;
    }
    
    function get_total_beban($id_frm_journal){
      $jumlah_debit = $this->db->query("
        SELECT SUM(A.saldo) AS price
        FROM frm_neraca_percobaan AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_frm_journal = {$id_frm_journal}
          AND B.labarugi = 2 AND B.profit = 1 AND B.pos = 1
        ")->row();
      $jumlah_debit_penyesuaian = $this->db->query("
        SELECT SUM(A.saldo) AS price
        FROM frm_neraca_penyesuaian AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_frm_journal = {$id_frm_journal}
          AND B.labarugi = 2 AND B.profit = 1 AND B.pos = 1
        ")->row();
      $debit = $jumlah_debit->price + $jumlah_debit_penyesuaian->price;
      return $debit;
    }
    
    function get_total_beban_yayasan($id_keuangan_yayasan_journal){
      $jumlah_debit = $this->db->query("
        SELECT SUM(A.saldo) AS price
        FROM keuangan_yayasan_neraca_percobaan AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
          AND B.labarugi = 2 AND B.profit = 1 AND B.pos = 1
        ")->row();
      $jumlah_debit_penyesuaian = $this->db->query("
        SELECT SUM(A.saldo) AS price
        FROM keuangan_yayasan_neraca_penyesuaian AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
          AND B.labarugi = 2 AND B.profit = 1 AND B.pos = 1
        ")->row();
      $debit = $jumlah_debit->price + $jumlah_debit_penyesuaian->price;
      return $debit;
    }
    
    function get_total_beban_lain($id_frm_journal){
      $jumlah_debit = $this->db->query("
        SELECT SUM(A.saldo) AS price
        FROM frm_neraca_percobaan AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_frm_journal = {$id_frm_journal}
          AND B.labarugi = 2 AND B.profit = 2 AND B.pos = 1
        ")->row();
      $jumlah_debit_penyesuaian = $this->db->query("
        SELECT SUM(A.saldo) AS price
        FROM frm_neraca_penyesuaian AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_frm_journal = {$id_frm_journal}
          AND B.labarugi = 2 AND B.profit = 2 AND B.pos = 1
        ")->row();
      $debit = $jumlah_debit->price + $jumlah_debit_penyesuaian->price;
      return $debit;
    }
    
    function get_total_beban_lain_yayasan($id_keuangan_yayasan_journal){
      $jumlah_debit = $this->db->query("
        SELECT SUM(A.saldo) AS price
        FROM keuangan_yayasan_neraca_percobaan AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
          AND B.labarugi = 2 AND B.profit = 2 AND B.pos = 1
        ")->row();
      $jumlah_debit_penyesuaian = $this->db->query("
        SELECT SUM(A.saldo) AS price
        FROM keuangan_yayasan_neraca_penyesuaian AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
          AND B.labarugi = 2 AND B.profit = 2 AND B.pos = 1
        ")->row();
      $debit = $jumlah_debit->price + $jumlah_debit_penyesuaian->price;
      return $debit;
    }
    
    function get_pendapatan_kotor($id_frm_journal){
      return ($this->get_total_pendapatan($id_frm_journal) - $this->get_total_beban($id_frm_journal));
    }
    
    function get_pendapatan_kotor_yayasan($id_keuangan_yayasan_journal){
      return ($this->get_total_pendapatan_yayasan($id_keuangan_yayasan_journal) - $this->get_total_beban_yayasan($id_keuangan_yayasan_journal));
    }
    
    function get_pendapatan_sebelum_pajak($id_frm_journal){
      $pendapatan_kotor = $this->get_pendapatan_kotor($id_frm_journal);
      $lain = $this->get_total_pendapatan_lain($id_frm_journal) - $this->get_total_beban_lain($id_frm_journal);
      return ($pendapatan_kotor + $lain);
    }
    
    function get_pendapatan_sebelum_pajak_yayasan($id_keuangan_yayasan_journal){
      $pendapatan_kotor = $this->get_pendapatan_kotor_yayasan($id_keuangan_yayasan_journal);
      $lain = $this->get_total_pendapatan_lain_yayasan($id_keuangan_yayasan_journal) - $this->get_total_beban_lain_yayasan($id_keuangan_yayasan_journal);
      return ($pendapatan_kotor + $lain);
    }
    
    function get_tanggungan_jenis_virtual($id_keuangan_mahasiswa_jenis_virtual_account, $nim){
      $debit = $this->global_models->get_query("SELECT SUM(saldo) AS sld FROM keuangan_mahasiswa_tanggungan
        WHERE id_keuangan_mahasiswa_jenis_virtual_account = {$id_keuangan_mahasiswa_jenis_virtual_account} AND nim = '{$nim}' AND (pos = 1 OR pos = 3)");
//      $debit = $this->global_models->get_field("keuangan_mahasiswa_tanggungan", "sum(saldo)", 
//              array("id_keuangan_mahasiswa_jenis_virtual_account" => $id_keuangan_mahasiswa_jenis_virtual_account, "nim" => $nim, "pos" => 1));
      $kredit = $this->global_models->get_field("keuangan_mahasiswa_tanggungan", "sum(saldo)", 
              array("id_keuangan_mahasiswa_jenis_virtual_account" => $id_keuangan_mahasiswa_jenis_virtual_account, "nim" => $nim, "pos" => 2));
      $hasil = $debit[0]->sld - $kredit;
      return $hasil;
    }
    
    function get_tanggungan_jenis_virtual_baru($id_keuangan_mahasiswa_jenis_virtual_account, $id_ao_applicant){
      $debit = $this->global_models->get_field("keuangan_mahasiswa_baru_tanggungan", "sum(saldo)", 
              array("id_keuangan_mahasiswa_jenis_virtual_account" => $id_keuangan_mahasiswa_jenis_virtual_account, "id_ao_applicant" => $id_ao_applicant, "pos" => 1));
      $kredit = $this->global_models->get_field("keuangan_mahasiswa_baru_tanggungan", "sum(saldo)", 
              array("id_keuangan_mahasiswa_jenis_virtual_account" => $id_keuangan_mahasiswa_jenis_virtual_account, "id_ao_applicant" => $id_ao_applicant, "pos" => 2));
      $hasil = $debit - $kredit;
      return $hasil;
    }
    
}

